package com.ycsj.goldmedalnewconcept.activity;

import java.io.IOException;

import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.ycsj.goldmedalnewconcept.constant.Constant;
import com.ycsj.goldmedalnewconcept.utils.SPUtil;
import com.ycsj.goldmedalnewconcept.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

//添加学生信息
public class UpdateStudentinfoActivity extends Activity implements OnClickListener {

	private ImageView img_back;
	private TextView txt_title;
	private EditText et_input;
	private Button bt_save;
	private String title;
	private int resultCode;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_updateuserinfo);
		title = getIntent().getStringExtra("title");
		resultCode = getIntent().getIntExtra("resultCode", -1);
		
		initView();
		setOnListener();
		initData();
	}

	private void initView() {
		img_back = (ImageView) findViewById(R.id.img_back);
		txt_title = (TextView) findViewById(R.id.txt_title);
		et_input = (EditText) findViewById(R.id.et_input);
		bt_save = (Button) findViewById(R.id.bt_save);
	}

	private void setOnListener() {
		img_back.setOnClickListener(this);
		bt_save.setOnClickListener(this);
	}

	private void initData() {
		txt_title.setText("添加" + title);
		et_input.setHint("输入" + title);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt_save:
			String trim = et_input.getText().toString().trim();
			
			if("".equals(trim)){
				Toast.makeText(this, "内容不能为空", 0).show();
				return;
			}
			
			  	Intent mIntent = new Intent();  
		        mIntent.putExtra("date", trim);  
		        // 设置结果，并进行传送  
		        this.setResult(resultCode, mIntent);
			
			finish();
			
			
//			if (!"".equals(trim)) {
////				Toast.makeText(UpdateUserinfoActivity.this, trim, 0).show();
//				OkHttpClient mOkHttpClient = new OkHttpClient();
////				mOkHttpClient.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
//				FormEncodingBuilder builder = new FormEncodingBuilder();
//				builder.add("type", "4");
//				builder.add("c", SPUtil.getString(UpdateStudentinfoActivity.this, "account", ""));
//				builder.add("role", SPUtil.getString(UpdateStudentinfoActivity.this, "role", ""));
//				builder.add("platform", "2");
//				if("姓名".equals(title)){
//					builder.add("name", trim);
//				}else{
//					builder.add("name", "");
//				}
//				if("电话".equals(title)){
//					builder.add("tel", trim);
//				}else{
//					builder.add("tel", "");
//				}
//				builder.add("birthday", "");
//				builder.add("sex", "");
//				builder.add("province", "");
//				builder.add("city", "");
//				builder.add("area", "");
//				Request request = new Request.Builder()
//				.url(Constant.SERVER_URL + "AccountMsg")
//				.post(builder.build()).build();
//		mOkHttpClient.newCall(request).enqueue(new Callback() {
//
//			@Override
//			public void onFailure(Request arg0, IOException arg1) {
//				
//			}
//
//			@Override
//			public void onResponse(Response arg0) throws IOException {
//				Looper.prepare(); 
//				Toast.makeText(UpdateStudentinfoActivity.this, "修改成功！", 0).show();
//				Looper.loop();
//				Log.e("test", arg0.body().string());
//			}
//			
//		});
//				
//			} else {
//
//			}
//			Intent intent =new Intent(UpdateStudentinfoActivity.this, UserInfoActivity.class);
//			startActivity(intent);
			break;
		case R.id.img_back:
			finish();
			break;
		default:
			break;
		}
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		finish();
	}

	
}
